package com.lagou.cachehomework.controller;

import com.lagou.cachehomework.entity.Recruit;
import com.lagou.cachehomework.repository.RecruitDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RecruitDao recruitDao;

//    @GetMapping("/put")
//    public String put(@RequestParam(required = true) String key
//            ,@RequestParam(required = true) String value){
//        redisTemplate.opsForValue().set(key,value,20,TimeUnit.SECONDS);
//        return "SUCCESS";
//    }

//    @GetMapping("/get")
//    public String get(@RequestParam(required = true) String key){
//        return (String)redisTemplate.opsForValue().get(key);
//    }

//    @GetMapping("/get")
//    public String get(@RequestParam(required = true) String key){
//        String value = (String) redisTemplate.opsForValue().get(key);
//        if (value == null || value == "") {
//            Optional<Recruit> optional = recruitDao.findById(Long.parseLong(key));
//            Recruit recruit = optional.get();
//            if(recruit!=null){
//                redisTemplate.opsForValue().set(key,recruit.toString(),60,TimeUnit.SECONDS);
//            }
//            return recruit.toString();
//        }
//        return (String) redisTemplate.opsForValue().get(key);
//    }
}
